$(function() {
	
	$("body").on("click",'.example5-3-2', function(){
		var menuUuid=$("input[name='menuUuid']").val();
		var goodsId=$(this).attr("id");
		var url='/goods/goodsDelete?goodsId='+goodsId+"&menuUuid="+menuUuid;
		deleteAlert(url);
	});
	
	$('body').on('change',".inputFileClass",function(){
		ext = $(this).val().substring($(this).val().lastIndexOf('.'),$(this).val().length).toUpperCase();  
	    //判定图片上传的格式
	    if(ext !== '.PNG' && ext !== '.JPG' && ext !== '.JPEG' && ext !== '.GIF'){  
	    	myErrorAlert("*图片限于bmp,png,gif,jpeg,jpg格式");
	        return false;  
	    }
	    var fd = new FormData();
	    fd.append('file', $(this)[0].files[0]);
	    var id=$(this).attr("id");
	    var action=$("#fileAction").val();
		$.ajax({
			type : "post",
			url : action,
			async : false,
			processData : false, // 不处理数据
			contentType : false, // 不设置内容类型
			data : fd,
			dataType : 'json', //返回类型json、text
			success : function(reslut) {
				if(isEmpty(reslut.data.realNetworkPath)){
					myErrorAlert("图片上传失败！");
					return false;
				}
				var imgId=id+"Img";
				var inputId=id+"Val";
				$("#"+imgId).attr("src", reslut.data.realNetworkPath);
				$("#"+inputId).val(reslut.data.picSavePath);
			},
			error : function() {
				myErrorAlert("图片上传失败！");
			}
		});
    });
	
	$("body").on("click",'.add_goodsSku', function(){
		var id=Number($(this).attr("id"))+1;
		$(this).attr("id",id);
		var tbody=$(this).parent().parent().parent();
		var trStr='<tr><td><input name="skuId" type="hidden" value=""/><img height="50px"id="inputFile'+id+'Img" src=" " width="50px"><input id="inputFile'+id+'Val"name="skuImg"type="hidden"></td><td><input class="inputFileClass"id="inputFile'+id+'"name="file"style="display: none"type="file"><input class="inputFileButton" id="'+id+'" type="button" value="添加规格图片"></td><td><input class="form-control"id="goodsSkuName"name="goodsSkuName"placeholder="商品规格名称"type="text"></td><td><input class="form-control"id="goodsSkuDisct"max="9999999999999"name="goodsSkuDisct"placeholder="门店价"type="NUMBER"></td><td><input class="form-control"id="goodsSkuPrice"max="9999999999999"name="goodsSkuPrice"placeholder="市场价"type="NUMBER"></td><td><input class="form-control"id="goodsSkuStock"name="goodsSkuStock"placeholder="库存"type="NUMBER"></td><td><a class="btn btn-block btn-danger delete_goodsSku" href="javascript:void(0);" id="11">删除</a></td></tr>';
		tbody.append(trStr);
	});
	
	$("body").on("click",'.delete_goodsSku', function(){
		$(this).parent().parent().remove();
	});
	
	$("body").on("click",'.inputFileButton', function(){
		var id=$(this).attr("id");
		document.getElementById('inputFile'+id).click()
	});
	
	$("body").on("click",'a[name="show"]',function(){
		
		$("#goodsIdSku").val("");
		$("#goodsNumSku").text("");
		$("#goodsNameSku").text("");
		var goodsId=$(this).attr("id");
    	var url="/goods/getGoodsSkuInfo";
    	var imgPathfile=$("#imgPathfile").val();
    	if(!isEmpty(goodsId)){
			var data = {
						'goodsId' :goodsId,
						};
			var datas=ajaxUtil(url, data, false, "","");
			if(!isEmpty(datas)){
				
				var goodskuList=datas.data.goodskuList;
				
				$("#goodsIdSku").val(datas.data.goodsId);
				$("#goodsNumSku").text(datas.data.goodsNum);
				$("#goodsNameSku").text(datas.data.goodsName);
				
				var tbody=$("#AddgoodsSkuPrice");
				tbody.children().remove();
				
				if(!isEmpty(goodskuList)){
					for (var i = 0; i < goodskuList.length; i++) {
						var skuImg =imgPathfile+goodskuList[i].skuImg;
						var skuId  =goodskuList[i].skuId;
						var goodsSkuDisct  =goodskuList[i].goodsSkuDisct;
						var goodsSkuPrice  =goodskuList[i].goodsSkuPrice;
						var goodsSkuName  =goodskuList[i].goodsSkuName;
						var trStr='<tr><td><input id="skuId" name="skuId" value="'+skuId+'" type="hidden" ><img class="lazy img-md" height="80px" width="80px" src="'+skuImg+'"></td><td>'+goodsSkuName+'</td><td><input class="form-control" id="goodsSkuDisct" max="9999999999999" name="goodsSkuDisct" value="'+goodsSkuDisct+'" placeholder="门店价" type="NUMBER"></td><td><input class="form-control" id="goodsSkuPrice" value="'+goodsSkuPrice+'" max="9999999999999" name="goodsSkuPrice" placeholder="市场价" type="NUMBER"></td></tr>';
						tbody.append(trStr);
					}
				}
				
				$('#goods_price_update').modal('show');
			}else{
				$('#goods_price_update').modal('hide');
			}
    	}
	});
	
	 $("body").on("click",'#updateGoodsSkuPriceButton', function(){
		 
		  var targetUrl = $("#formGoodsSku").attr("action");    
		  var data = $("#formGoodsSku").serialize();     
		   $.ajax({ 
		    type:'post',  
		    url:targetUrl, 
		    cache: false,
		    data:data,  //重点必须为一个变量如：data
		    dataType:'json', 
		    success:function(data){
		    	if(data.status!=200){
	        		myErrorAlert(data.msg);
	        		return false;
	        	}else{
	        		mySuccessAlert("修改成功！");
	        		$('#goods_price_update').modal('hide');
	        	}
		    },
		    error:function(){ 
		    	myErrorAlert("系统繁忙！")
		    }
		   })
	 });
})